package com.zone.core.ext

import com.google.gson.Gson
import com.zone.core.CoreApplication
import java.lang.reflect.Type

/**
 *Author: last
 *Time: 2020-01-16
 *Description: 对String的扩展
 */

fun String.between(startStr: String = "", endStr: String = ""): String {
    val start = if (startStr.isEmpty()) {
        0
    } else {
        this.indexOf(startStr) + startStr.length
    }
    val end = if (endStr.isEmpty()) {
        this.length
    } else {
        this.indexOf(endStr)
    }
    return this.substring(start, end)
}

fun String.assetsOpen(): String {
    return CoreApplication.coreApp.assets.open(this).bufferedReader().use {
        it.readText()
    }
}

fun String.fromJson(clazz: Class<*>): Any {
    return Gson().fromJson(this.replace(Regex("\\s+"), ""), clazz,)
}
fun <T> String.fromJson(typeOfT: Type): T {
    return Gson().fromJson<T>(this.replace(Regex("\\s+"), ""), typeOfT)
}